<?php

namespace app\api\controller\order;

use app\api\controller\Controller;
use app\api\model\order\Cart as CartModel;
use app\api\model\settings\Setting as SettingModel;
use app\api\model\product\Product as ProductModel;
use app\common\model\order\OrderProduct;
use app\common\model\order\CartOrder;
/**
 * 购物车控制器
 */
class Cart extends Controller
{
    private $user;

    // $model
    private $model;
    // 当前应用id
    protected $app_id;
    /**
     * 构造方法
     */
    public function initialize()
    {
        $this->user = $this->getUser();
        $this->model = new CartModel();
    }

    /**
     * 购物车列表
     */
    public function lists()
    {
        // 购物车商品列表
        $productList = $this->model->getList($this->user);
        // 会员价
        $product_model = new ProductModel();
        foreach ($productList as $supplier) {
            foreach ($supplier['productList'] as $product) {
                $product_model->setProductGradeMoney($this->user, $product);
            }
        }
        //是否显示店铺信息
        $store_open = SettingModel::getStoreOpen();
        return $this->renderSuccess('', compact('productList', 'store_open'));
    }

    /**
     * 加入购物车
     * @param int $product_id 商品id
     * @param int $product_num 商品数量
     * @param string $product_sku_id 商品sku索引
     */
    public function add()
    {
        $data = $this->request->param();
        $product_id = $data['product_id'];
        $product_num = $data['total_num'];
        $spec_sku_id = $data['spec_sku_id'];
        $model = $this->model;
        if (!$model->add($this->user, $product_id, $product_num, $spec_sku_id)) {
            return $this->renderError($this->model->getError() ?: '加入购物车失败');
        }
        // 购物车商品总数量
        $totalNum = $model->getProductNum($this->user);
        return $this->renderSuccess('加入购物车成功', ['cart_total_num' => $totalNum]);
    }



    public function addOrder()
    {
        $OrderProduct = new OrderProduct();
        $order_id = $this->request->param('order_id');
        $CartOrder = new CartOrder();
        $user_id = $this->user['user_id'];

        if($order_id){
            $field = 'product_id,total_num,spec_sku_id';
            $list = $OrderProduct -> where('order_id',$order_id) -> field($field) -> select() -> toArray();

            if(!$list){
                return $this->renderError('订单不存在');
            }
            $cart_id_array = [];
            $cart_order = $CartOrder -> where('order_id',$order_id) -> find();
            $count = 0;
            if($cart_order){ //如果查询购物车是否还存在
                $count = $this->model -> where('cart_id','in',$cart_order['cart_ids']) -> count();
            }
            if($count){ //如果已经存在就不在存入购物车
                $cart_id_array = $cart_order['cart_ids'];
            }else{
                foreach ($list as $value){
                    $res = $this->model->add($this->user, $value['product_id'], $value['total_num'], $value['spec_sku_id']);
                    if($res){
                        $cart_id_array[] = $res;
                    }
                }

                if(!$cart_order){
                    $CartOrder -> createData([
                        'order_id' => $order_id,
                        'cart_ids' => $cart_id_array
                    ]);
                }else{
                    $cart_id_str = json_encode($cart_id_array);
                    $CartOrder -> where('order_id',$order_id) -> update(['cart_ids' =>$cart_id_str]);
                }

            }

        }else{
            return $this->renderError('订单ID不能为空');
        }

        $cart_id_str = implode(',', $cart_id_array);
        // 购物车商品总数量
        return $this->renderSuccess('加入购物车成功', ['cart_id' => $cart_id_str]);
    }

    /**
     * 减少购物车商品数量
     * @param $product_id
     * @param $product_sku_id
     * @return array
     */
    public function sub($product_id, $spec_sku_id)
    {
        $this->model->sub($this->user, $product_id, $spec_sku_id);
        return $this->renderSuccess('');
    }

    /**
     * 删除购物车中指定商品
     * @param $product_sku_id (支持字符串ID集)
     * @return array
     */
    public function delete($cart_id)
    {
        $this->model->setDelete($this->user, $cart_id);
        return $this->renderSuccess('删除成功');
    }
}